const Clean = require('clean-webpack-plugin'),
  ExtractTextPlugin = require("extract-text-webpack-plugin"),
  path = require('path'),
  webpack = require('webpack'),
  glob = require('glob'),
  CopyWebpackPlugin = require('copy-webpack-plugin')


let files = glob('./src/scripts/' + "*.js", {sync: true}),
  entry = {};

files.forEach(function (file) {

  let fileName = /([^\/.]+).js$/ig.exec(file)[1];
  entry[fileName] = file;

});

module.exports = {
  entry,
  output: {
    path: path.resolve(__dirname, 'dist/'),
    filename: '[name].js',
    chunkFilename: '[name].[chunkhash:5].js',
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      },{
        test: /\.js(x)*$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['react', 'stage-1', 'es2015'],
              plugins: [
                'syntax-decorators',
                'transform-class-properties',
                "transform-object-rest-spread",
                'transform-object-assign'
              ]
            }
          }
        ],
        include: [
          path.join(process.cwd(), './src'),
        ]
      },
      {
        test: /\.(png|jpg|gif|ttf|eot|svg|woff)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      },
      {
        test: require.resolve('jquery'),
        use: [{
          loader: 'expose-loader',
          options: '$'
        }]
      }
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({ name: 'commons', filename: 'commons.js' }),
    new Clean(['dist']),
    new ExtractTextPlugin("[name].css"),
    new webpack.ProvidePlugin({
      $: 'jquery'
    }),
    new CopyWebpackPlugin([
      { from: './*.html', to: './' },
      { from: './src/lib/', to: './' },
    ])
  ],
  devServer: {
    // host: '192.168.9.209',
    port: 7000
  }
};
